{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting data/train-images-idx3-ubyte.gz\n",
      "Extracting data/train-labels-idx1-ubyte.gz\n",
      "Extracting data/t10k-images-idx3-ubyte.gz\n",
      "Extracting data/t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets\n",
    "tf.set_random_seed(0)\n",
    "mnist = read_data_sets(\"data\", one_hot=True, reshape=False, validation_size=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9747\n"
     ]
    }
   ],
   "source": [
    "def nn_2_1():\n",
    "    \n",
    "    #remove logs\n",
    "    logdir = 'logs_2_1/'\n",
    "    maxstep = 10000+1\n",
    "    learning_rate = 0.005\n",
    "    \n",
    "    if tf.gfile.Exists(logdir):\n",
    "        tf.gfile.DeleteRecursively(logdir)\n",
    "        tf.gfile.MakeDirs(logdir)\n",
    "\n",
    "    sess = tf.InteractiveSession()\n",
    "    \n",
    "    L = 200\n",
    "    X = tf.placeholder(tf.float32, [None, 28, 28, 1])\n",
    "    Y_ = tf.placeholder(tf.float32, [None, 10])\n",
    "    W1 = tf.Variable(tf.truncated_normal([784, L],stddev=0.1))\n",
    "    B1 =  tf.Variable(tf.ones([L])/10)\n",
    "    \n",
    "    W2 = tf.Variable(tf.truncated_normal([L, 10],stddev=0.1))\n",
    "    B2 = tf.Variable(tf.ones([10])/10)\n",
    "\n",
    "    XX = tf.reshape(X, [-1, 784])\n",
    "\n",
    "    Y1 = tf.nn.sigmoid(tf.matmul(XX, W1) + B1)\n",
    "    Y = tf.nn.softmax(tf.matmul(Y1, W2) + B2)\n",
    "\n",
    "    cross_entropy = -tf.reduce_sum(Y_*tf.log(Y))\n",
    "    correct_prediction = tf.equal(tf.argmax(Y_,1), tf.argmax(Y, 1))\n",
    "    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "\n",
    "     #summary\n",
    "    summary_cross_entropy =  tf.summary.scalar(\"cross_entropy\", cross_entropy)\n",
    "    summary_accuracy = tf.summary.scalar('accuracy', accuracy)\n",
    "    merged_summary_op = tf.summary.merge([summary_cross_entropy, summary_accuracy])\n",
    "    train_writer = tf.summary.FileWriter(logdir+'train/', sess.graph)\n",
    "    test_writer = tf.summary.FileWriter(logdir+ 'test/')\n",
    "    \n",
    "    # training, learning rate = 0.005\n",
    "    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)\n",
    "    tf.global_variables_initializer().run()\n",
    "    \n",
    "    for i in range(maxstep):\n",
    "        batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "        sess.run(train_step, feed_dict={X: batch_xs, Y_: batch_ys})\n",
    "        \n",
    "        if i %100 == 0:\n",
    "                summary, acc= sess.run([merged_summary_op, accuracy], feed_dict ={X: mnist.test.images, Y_: mnist.test.labels})\n",
    "                test_writer.add_summary(summary, i)\n",
    "                \n",
    "        if i % 100 == 0:\n",
    "                summary, step = sess.run([merged_summary_op, train_step],  feed_dict={X: batch_xs, Y_: batch_ys})\n",
    "                train_writer.add_summary(summary, i)\n",
    "                \n",
    "    print sess.run(accuracy, feed_dict={X: mnist.test.images, Y_: mnist.test.labels})\n",
    "    train_writer.close()\n",
    "    test_writer.close()                             \n",
    "    sess.close()\n",
    "\n",
    "    \n",
    "nn_2_1()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
